package com.ymtms.arrivalofgoods.controller;

import com.github.pagehelper.PageInfo;
import com.ymtms.arrivalofgoods.bo.GridRequest;
import com.ymtms.arrivalofgoods.bo.GridResult;
import com.ymtms.arrivalofgoods.pojo.ArriveError;
import com.ymtms.arrivalofgoods.pojo.DriverInformation;
import com.ymtms.arrivalofgoods.pojo.GoodsbillReceoptMain;
import com.ymtms.arrivalofgoods.service.ArriveErrorService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@CrossOrigin
public class ArriveErrorContrller {
    @Autowired
    private ArriveErrorService arriveErrorService;
    @Value("${static.filepath}")
    private String filepath;

    /**
     * Discription Todo差错信息的上传，及添加
     *
     * @return
     * @Param
     * @author Administrator
     * @date 2019/10/24 11:40
     **/
    @ResponseBody
    @PostMapping("/saveArriveError")
    public Map saveArriveError(Integer num,ArriveError arriveError, @RequestParam("file") MultipartFile file) {
        System.out.println(num);
        Map map = new HashMap();
        try {
            arriveErrorService.saveArriveService(num,arriveError, file, filepath);
            map.put("code", "0");
            map.put("msg", "success");
        } catch (Exception e) {
            e.printStackTrace();
            map.put("code", "500");
            map.put("msg", "failed");
        }
        return map;
    }

    /**
     * Discription Todo差错信息的上传，及添加
     *
     * @return
     * @Param
     * @author Administrator
     * @date 2019/10/24 11:40
     **/
    @ResponseBody
    @PostMapping("/getArriveError")
    public Map saveArriveError(ArriveError arriveError, @RequestParam("file") MultipartFile file) {
        Map map = new HashMap();
        try {
            arriveErrorService.saveArriveService(arriveError, file, filepath);
            map.put("code", "0");
            map.put("msg", "success");
        } catch (Exception e) {
            e.printStackTrace();
            map.put("code", "500");
            map.put("msg", "failed");
        }
        return map;
    }
    /**
     * Discription Todo差错信息的下载
     *
     * @return
     * @Param
     * @author Administrator
     * @date 2019/10/24 11:40
     **/
    @ResponseBody
    @RequestMapping("/download/{aid}")
    public ResponseEntity<byte[]> download(@PathVariable("aid") Integer aid) {
        System.out.println("eeeeee");
        ArriveError arriveError = arriveErrorService.findArriveErrorByAid(aid);
        String packagePath = arriveError.getPackagePath();
        String filename = packagePath.substring(packagePath.indexOf("/") + 1);
        try {
            FileInputStream fileInputStream = new FileInputStream(new File(packagePath));
            byte[] body = new byte[fileInputStream.available()];
            fileInputStream.read(body);
            HttpHeaders headers = new HttpHeaders();
            headers.setContentDispositionFormData("attachment", filename);
            ResponseEntity responseEntity = new ResponseEntity(body, headers, HttpStatus.OK);
            return responseEntity;

        } catch (Exception e) {
            e.printStackTrace();
        }

        return null;
    }



    @ResponseBody
    @RequestMapping("/showError")
    public GridResult<ArriveError> seekReceipt(
            GridRequest gridRequest,// 封装页面中laiyu表格所传过来的基本信息
           ArriveError arriveError
           ) {
        try {
            System.out.println("mmmmmmmmmmmm");

            Integer currentPage = gridRequest.getPage();
            Integer pageSize = gridRequest.getLimit();


            List<ArriveError> arriveErrors = arriveErrorService.queryAllArriveError(currentPage, pageSize, arriveError);
            PageInfo<ArriveError> pageInfo = new PageInfo<>(arriveErrors);
            GridResult<ArriveError> grid = new GridResult<>();

            // 存入查询数据
            grid.setData(arriveErrors);
            // 存入总条数
            grid.setCount(pageInfo.getTotal());
            grid.setCode(0);
            grid.setMsg("");
            return grid;
        } catch (Exception e) {
            e.printStackTrace();
        }

        return null;

    }


    @PostMapping("/deleteErrorBatch/{num}")
    @ResponseBody
    public Map delete(@PathVariable("num") Integer num) {
        Map result = new HashMap();
        try {
            arriveErrorService.deleteReceiptByNum(num);
            result.put("code", "0");
            result.put("msg", "删除成功");
        } catch (Exception e) {
            e.printStackTrace();
            result.put("code", "500");
            result.put("msg", "删除失败");
        }
        return result;
    }


    @ResponseBody
    @RequestMapping("/modifyArriveError")
    public Map modifyReceipt(ArriveError arriveError) {

        Map map = new HashMap();
        try {
            arriveErrorService.modifyArriveError(arriveError);
            map.put("code", "0");
            map.put("msg", "成功");
        } catch (Exception e) {
            e.printStackTrace();
            map.put("code", "500");
            map.put("msg", "失败");
        }
        return map;

    }


}
